package com.ailianlian.ablecisi.adapter;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ailianlian.ablecisi.R;
import com.ailianlian.ablecisi.baseclass.BaseAdapter;
import com.ailianlian.ablecisi.pojo.entity.Comment;
import com.ailianlian.ablecisi.utils.ImageLoader;
import com.google.android.material.imageview.ShapeableImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论列表适配器
 */
public class CommentAdapter extends BaseAdapter<Comment, CommentAdapter.CommentViewHolder> {

    private final List<Comment> comments = new ArrayList<>();
    private OnCommentActionListener listener;

    public CommentAdapter(Context context) {
        super(context);
    }

    public interface OnCommentActionListener {
        void onReplyClicked(Comment comment);
        void onLikeClicked(Comment comment);
    }

    public void setOnCommentActionListener(OnCommentActionListener listener) {
        this.listener = listener;
    }

    public void submitList(List<Comment> newComments) {
        this.comments.clear();
        if (newComments != null) {
            this.comments.addAll(newComments);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
        holder.bind(comments.get(position));
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }

    public class CommentViewHolder extends RecyclerView.ViewHolder {
        private final ShapeableImageView ivUserAvatar;
        private final TextView tvUserName;
        private final TextView tvCommentTime;
        private final TextView tvCommentContent;
        private final TextView tvLikeCount;
        private final TextView btnReply;
        private final LinearLayout layoutReplies;

        public CommentViewHolder(@NonNull View itemView) {
            super(itemView);
            ivUserAvatar = itemView.findViewById(R.id.ivUserAvatar);
            tvUserName = itemView.findViewById(R.id.tvUserName);
            tvCommentTime = itemView.findViewById(R.id.tvCommentTime);
            tvCommentContent = itemView.findViewById(R.id.tvCommentContent);
            tvLikeCount = itemView.findViewById(R.id.tvLikeCount);
            btnReply = itemView.findViewById(R.id.btnReply);
            layoutReplies = itemView.findViewById(R.id.layoutReplies);
        }

        public void bind(Comment comment) {
            // 设置用户头像
            ImageLoader.load(itemView.getContext(), comment.getUserAvatarUrl(), ivUserAvatar, R.drawable.ic_profile, R.drawable.ic_profile);

            tvUserName.setText(comment.getUserName());// 设置用户名
            tvCommentTime.setText(formatTimeAgo(comment.getCreateTime()));// 设置评论时间
            tvCommentContent.setText(comment.getContent());// 设置评论内容
            tvLikeCount.setText(String.valueOf(comment.getLikeCount()));// 设置点赞数

            // 设置点赞状态
            if (comment.getLiked()) {
                tvLikeCount.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_heart_filled, 0, 0, 0);
                tvLikeCount.setTextColor(itemView.getContext().getResources().getColor(R.color.primary, null));
            } else {
                tvLikeCount.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_heart, 0, 0, 0);
                tvLikeCount.setTextColor(itemView.getContext().getResources().getColor(R.color.text_secondary, null));
            }

            // 设置回复按钮点击事件
            btnReply.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onReplyClicked(comment);
                }
            });

            // 设置点赞按钮点击事件
            tvLikeCount.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onLikeClicked(comment);
                }
            });

            // 添加回复列表
            layoutReplies.removeAllViews();
            if (comment.getReplies() != null && !comment.getReplies().isEmpty()) {
                for (Comment reply : comment.getReplies()) {
                    View replyView = LayoutInflater.from(itemView.getContext())
                            .inflate(R.layout.item_comment_reply, layoutReplies, false);
                    bindReplyView(replyView, reply);
                    layoutReplies.addView(replyView);
                }
            }
        }

        private void bindReplyView(View replyView, Comment reply) {
            ShapeableImageView ivReplyUserAvatar = replyView.findViewById(R.id.ivUserAvatar);
            TextView tvReplyUserName = replyView.findViewById(R.id.tvUserName);
            TextView tvReplyCommentTime = replyView.findViewById(R.id.tvCommentTime);
            TextView tvReplyCommentContent = replyView.findViewById(R.id.tvCommentContent);
            TextView tvReplyLikeCount = replyView.findViewById(R.id.tvLikeCount);
            TextView btnReplyToReply = replyView.findViewById(R.id.btnReply);

            // 设置用户头像
            ImageLoader.load(itemView.getContext(), reply.getUserAvatarUrl(), ivReplyUserAvatar, R.drawable.ic_profile, R.drawable.ic_profile);
            tvReplyUserName.setText(reply.getUserName());// 设置用户名
            tvReplyCommentTime.setText(formatTimeAgo(reply.getCreateTime()));// 设置评论时间
            tvReplyCommentContent.setText(reply.getContent());// 设置评论内容
            tvReplyLikeCount.setText(String.valueOf(reply.getLikeCount()));// 设置点赞数

            // 设置点赞状态
            if (reply.getLiked()) {
                tvReplyLikeCount.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_heart_filled, 0, 0, 0);
                tvReplyLikeCount.setTextColor(itemView.getContext().getResources().getColor(R.color.primary, null));
            } else {
                tvReplyLikeCount.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_heart, 0, 0, 0);
                tvReplyLikeCount.setTextColor(itemView.getContext().getResources().getColor(R.color.text_secondary, null));
            }

            // 设置回复按钮点击事件
            btnReplyToReply.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onReplyClicked(reply);
                }
            });

            // 设置点赞按钮点击事件
            tvReplyLikeCount.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onLikeClicked(reply);
                }
            });
        }

        private String formatTimeAgo(long timeMillis) {
            long now = System.currentTimeMillis();
            CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
                    timeMillis, now, DateUtils.MINUTE_IN_MILLIS,
                    DateUtils.FORMAT_ABBREV_RELATIVE);
            return timeAgo.toString();
        }
    }
} 